define(function(require, exports){
	var $ = require('jquery');
	var util = require('util');
	var pubjs = require('pubjs');
	var view = require('@base/view');

	// 基本的radio, 只有选项
	var RadioGroup = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'M-radioGroup',
				'url': '',
				'param': null,		//请求参数
				'items': null,
				'id': '',
				'name': '',
				'reqType': 'ajax',
				'tplFile':	'/tpl/component/radioGroup.html',
				'silent': true,	// 是否发消息
				'fireType': 'key',	// 发送事件时,是发key, 还是对象, //fireType: key, object
				'prefix': '',	// 生成id的前缀
				'allItem': null,	//代表所有的选择项.
				'defaultValue': '',	//默认的值.
				'view_model': {
					'items': [],	//全部项
					'prefix': '',
					'readonly': false,
					'data': '',		//选中的id集合
					'id': '',		//id field name
					'name': '',		//name filed name
					'itemClick': this.vmItemClick.bind(this)
				}
			});
			this.Super('init', arguments);
		},
		afterBuild: function(){
			var vm = this.vm.$;
			var c = this.getConfig();
			vm['id'] = c.id || 'id';
			vm['name'] = c.name || 'name';
			vm['prefix'] = c.prefix || Math.floor(Math.random()*10000);
			vm['data'] = c.defaultValue ? [c.defaultValue.toString()] : '';
			this.buildFromTemplate();
		},
		afterBuildTemplate: function(){
			var c = this.getConfig();
			if(c.url){
				this.load();
			}else if(c.items){
				if(c.allItem) {
					c.items.unshift(c.allItem);
				}
				this.vm.$.items = c.items;
			}
		},
		vmItemClick: function(value) {
			var c = this.getConfig();
			var vm = this.vm.$;
			if(!c.mul) {
				vm.data = [value.toString()];
			}

			if(!c.silent){
				this.fire('radioGroupClick', this.getValue(c.fireType == 'object'));
			}
			return false;
		},
		load: function( param ) {
			var c = this.getConfig();
			pubjs.sync(null);
			if( c.reqType === 'ajax' ) {
				pubjs.data.get( c.url, param || c.param, this );
			}
			else if( c.reqType === 'websocket' ) {
				pubjs.mc.send(
					c.url,
					$.extend({}, c.param ),
					this.onData.bind( this )
				);
			}
		},
		onData: function(err, data ) {
			var c = this.getConfig();
			pubjs.sync(true);
			if(err){
				pubjs.error(err);
				return false;
			}
			if(data.items) {
				data = data.items;
			}
			if(c.allItem) {
				data.unshift(c.allItem);
			}
			this.vm.$.items = data;
		},
		getValue: function(bool) {
			// bool  true: 返回name id, false返回id.
			var ret = this.vm.getItem('data');
			ret = ret && ret[0];
			if(!ret) {
				return false;
			}
			var id = this.vm.getItem('id');
			if(bool) {
				var items = this.vm.getItem('items');
				ret = util.find(items, ret, id);
			}
			return ret;
		},
		setData: function(data) {
			this.vm.$.items = this.$data = data;
			return this;
		},
		setValue: function(data) {
			this.vm.$.data = [data.toString()];
			return this;
		},
		setViewMode: function(view) {
			this.vm.setItem('readonly', !!view);
			return this;
		},
		reset: function() {
			var c = this.getConfig();
			this.vm.$.data = c.defaultValue ? [c.defaultValue.toString()] : '';
			return this;
		}
	});
	exports.base = RadioGroup;

});